<?php
//模块名_控制名
class Product_ReturnsController extends Zend_Controller_Action
{
	public function preDispatch(){
		$this->tplDirectory = "product/returns/";
	}
	public function listAction(){
		$this->view->status = Product_Service_Receiving::$statusText;
		echo Cff::renderTpl($this->tplDirectory.'index.tpl',"layout");
	}
	
	public function loadListAction(){
		$receivingCode = trim($this->_request->getParam("receivingCode",""));
		$status = trim($this->_request->getParam("status",""));
		$refId = trim($this->_request->getParam("refId",""));
		$customerId = trim($this->_request->getParam("customerId",""));
		$page = trim($this->_request->getParam("page",1));
		$pageSize = trim($this->_request->getParam("pageSize",20));
		$sortColumn = trim($this->_request->getParam("sortColumn",""));
		$sortMethod = trim($this->_request->getParam("sortMethod","asc"));
		$condition = array(
				"receiving_code" => $receivingCode,
				"status" => $status,
				"ref_id" => $refId,
				"customer_id" => $customerId
		);
		$orderby = array();
		$orderby[] = 'created desc';
		if($sortColumn){
			$sortStr = $sortColumn.' '.$sortMethod; 
			array_unshift($orderby,$sortStr);
		}
		
		$total = Product_Service_Receiving::getReturnsByCondition($condition,"count(*)");
		$list = Product_Service_Receiving::getReturnsByCondition($condition,"*",$page,$pageSize,$orderby);
		$return = array("ask" => false,"total" => $total);
		if(!empty($list)){
			$return['ask'] = true;
			$return['result'] = $list;
		}
		die(json_encode($return));
	}
	
	public function packageProccessCheckAction(){	//检查用户退货处理方式
		$receiving_code = trim($this->_request->getParam("receiving_code",""));
		Product_Service_Receiving::changeStatus($receiving_code,2);
		$obj = new Product_Service_RmaProccess('receiving_code',$receiving_code);
		
		$customerInfo = $obj->getCustomerInfo();
		$data = array('ask'=>0);
		if(isset($customerInfo) && $customerInfo['return_package_proccess']==0){
			$data = array('ask'=>1,'receiving_code'=>'','msg'=>array('用户处理方式为销毁'));			
		}
		die(json_encode($data));
	}
	
	public function detailAction(){
		$receivingCode = trim($this->_request->getParam("code",""));			
		$condition = array("receiving_code" => $receivingCode);
		Product_Service_Receiving::changeStatus($receivingCode,2);
				
		$receiving = Product_Service_Receiving::getReturnsByCondition($condition);
		$items = array();
		if(is_array($receiving) && !empty($receiving)){
			$items = Product_Service_Receiving::getItems($condition);
		}
		$session = new Zend_Session_Namespace('userAuth');
		$this->view->userName=$session->name;
		$this->view->currentDate = date("Y年m月d日");
		
		$this->view->receiving = $receiving[0];
		$this->view->items = $items;
		echo $this->view->render($this->tplDirectory.'detail.tpl');
	}
	
	public function checkHasFirstAction(){
		$receivingCode = trim($this->_request->getParam("code",""));
		$condition = array("receiving_code" => $receivingCode);
		$receiving = Product_Service_Receiving::getByCondition($condition);
		$items = array();
		$hasFirst = false;//默认不是第一次收货
		if(is_array($receiving) && !empty($receiving)){
			$items = Product_Service_Receiving::getItems($condition);
		}
		foreach($items as $key => $value){
			$items[$key]['product']['package'] = Product_Service_Product::getByProductIdAndWarehouseId($value['product']['product_id'],$receiving[0]['warehouse_id']);
			if($value['product']['product_warehouse_status'] == 0){//第一次收货
				$hasFirst = true;
			}
		}
		die(json_encode($hasFirst));
	}
	
	public function receiveAction(){
		$receivingCode = trim($this->_request->getParam("code",""));
		$condition = array("receiving_code" => $receivingCode);
		$receiving = Product_Service_Receiving::getReturnsByCondition($condition);
		$items = array();
		$hasFirst = false;//默认不是第一次收货
		$firstItems = array();
		if(is_array($receiving) && !empty($receiving)){
			$items = Product_Service_Receiving::getItems($condition);
		}
		foreach($items as $key => $value){
			$items[$key]['product']['package'] = Product_Service_Product::getByProductIdAndWarehouseId($value['product']['product_id'],$receiving[0]['warehouse_id']);
			if($value['product']['product_warehouse_status'] == 0){//第一次收货
				$hasFirst = true;
				$firstItems[] = $items[$key];
			}
		}
		$categorys = Product_Service_Product::getAllCategory();
		$this->view->categorys = $categorys;
		$packages = Product_Service_Package::getAllAvailable($receiving[0]['warehouse_id'],$receiving[0]['customer_id']);
		$this->view->packages = $packages;
		$this->view->receiving = $receiving[0];
		if($hasFirst){
			$this->view->items = $firstItems;
			echo $this->view->render($this->tplDirectory.'first.tpl');
			exit;
		}
		$this->view->items = $items;
		echo $this->view->render($this->tplDirectory.'receive.tpl');
	}
	
	public function itemInfoAction(){
		$itemId = trim($this->_request->itemId);
		$item = Product_Service_Receiving::getItemByItemId($itemId);
	
		$return = array("ask" => false);
		if($item){
			$return["ask"] = true;
			$return["result"] = $item;
		}
		die(json_encode($item));
	}
	
	public function receiveAddAction(){
		$itemId = trim($this->_request->itemId);
		$weight = trim($this->_request->weight);
		$receiveQty = trim($this->_request->receiveQty);
		$nonLabelQty = trim($this->_request->nonLabelQty);
		$isPacket = trim($this->_request->isPacket);
		$return_result = trim($this->_request->return_result);
		$remark = trim($this->_request->remark);
		$labeled = 1;
		if($nonLabelQty != 0){
			$labeled = 0;
		}
		$row = array(
			"ri_id" => $itemId,
			"weight" => $weight,
			"received_qty" => $receiveQty,
			"non_labeled_qty" => $nonLabelQty,
			"labeled" => $labeled,
			"packaged" => $isPacket,
			"return_result" => $return_result,
			"remark" => $remark,
		);
		$result = Product_Service_Receiving::receiveAdd($row);
		$return = array("ask" => false);
		if(isset($result['code'])){
			$return["message"] = $result['message'];
			die(json_encode($return));
		}
		if($result){
			$return["ask"] = true;
			$return["item"] = Product_Service_Receiving::getItemByItemId($itemId);
			$receiving_code = $return['item']['receiving_code'];
			$customer_id = $return["item"]['product']['customer_id'];
			$obj = new Product_Service_RmaProccess();
			$res = $obj->updatePlanItem($customer_id,'item',$receiveQty,$receiving_code);
		}
		die(json_encode($return));
	}
	
	public function confirmProductAction(){
		$productId = $this->_request->productId;
		$result = array("ask" => Product_Service_Receiving::confirmProduct($productId));
		die(json_encode($result));
	}
	
	public function editProductAction(){
		$productId = trim($this->_request->productId);
		$category = trim($this->_request->category);
		$declared = trim($this->_request->declared);
		$packet = trim($this->_request->packet);
		$weight = trim($this->_request->weight);
		$warehouseId = trim($this->_request->warehouseId);
		$row = array(
			"productId" => $productId,
			"category" => $category,
			"declared" => $declared,
			"packet" => $packet,
			"weight" => $weight,
			"warehouseId" => $warehouseId,
		);
		$result = Product_Service_Receiving::editProduct($row);
		if(isset($result['code'])){
			$return["ask"] = false;
			$return["message"] = $result['message'];
			die(json_encode($return));
		}
		$return = array("ask" => Product_Service_Receiving::editProduct($row));
		die(json_encode($return)); 
	}
	
	public function barcodeAction(){
		Product_Service_Barcode::barcode($this->_request->code);
	}
	
	public function rmaCreateAction(){
		$orders_code = $this->_request->getParam('orders_code','');
		
		$error = Product_Service_RmaProccess::checkOrderCode($orders_code);
		if(count($error)){
			die(json_encode(array('ask'=>0,'receiving_code'=>'','msg'=>$error)));
		}
		
		$receivingArr = $productArr = array();
		$receivingArr['warehouse_id'] = 1;  	//只有一个仓库，ID为 1
		$receivingArr['order_type'] = 2;		// order_type 为 2，订单退件
		$receivingArr['ref_id'] = $orders_code; 
		$receivingArr['status'] = 1; 					//1 等待状态
		$receivingArr['created'] = date('Y-m-d H:i:s');			
		$receivingArr['pickup'] = 0;		//暂时默认为0
		$receivingArr['supplier_id'] = '';		//供应商ID 暂时为空
		$receivingArr['description'] = '';		//
		
		$products = Order_Service_Product::getByfield('orders_code',$orders_code);	//订单对应的产品信息
		foreach ($products as $key => $val){
			$productArr[$val['product_id']] = $val['op_quantity'];				
		}
		$data = array('ask'=>0,'receiving_code'=>'','msg'=>array());
		
		$obj = new Product_Service_RmaProccess('orders_code',$orders_code);
		$tmpArr = $obj->create($productArr,$receivingArr);
		$tmpArr && $data = $tmpArr;
		
		die(json_encode($data));
	}
	
}